package com.loie.xcx.web.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.config.TaxCloudConfig;
import com.loie.xcx.model.bankdto.ResultDTO;
import com.loie.xcx.model.dto.IwinstanceQueryDTO;
import com.loie.xcx.service.BuyerCodeApplyService;
import com.loie.xcx.syUtil.PostmanCodeGenerator;
import com.loie.xcx.util.ErrorMessage;
import com.loie.xcx.util.HttpUtil;
import com.sinosure.edi.polo.BuyerCodeApplyInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-02-17
 */
@Slf4j
@RestController
@RequestMapping("/xingbo")
public
class BuyerApplycodeController {

    //    @Autowired
//    BuyerCodeReplyService buyerCodeReplyService;
    @Autowired
    TaxCloudConfig taxCloudConfig;
    @Autowired
    private BuyerCodeApplyService buyerCodeApplyService;
    //private static final Logger logapp = LoggerFactory.getLogger("applog");
    private static final Logger zxbLogger = LoggerFactory.getLogger("zxblog");


    @PostMapping("/buycodecreateposman")
    public
    JsonResult<JSONObject> buycodecreateposman(@RequestBody String jsonStr) throws Exception {
        IwinstanceQueryDTO quotationQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        String recordid = "";
        if (org.apache.commons.lang3.StringUtils.isNotBlank(quotationQueryDTO.getRecordId()))
            recordid = quotationQueryDTO.getRecordId();
        List<BuyerCodeApplyInfo> buyerCodeApplyInfoList = buyerCodeApplyService.getBuyerCodeApplylist("");
        if (buyerCodeApplyInfoList.size() > 0) {
            String url = "{{host}}/ediserver/gateway.do";
            JSONArray headers = new JSONArray();
            headers.add(PostmanCodeGenerator.createHeader("Content-Type", "application/json"));
            //String body = "{\"key\":\"value\"}";
            JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(buyerCodeApplyInfoList));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("datas", jsonArray);
            jsonObject.put("imethod", "doBprBuyerCodeApply");
            HashMap<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json");

            String path=System.getProperty("user.dir")+"\\zxbbankAPI\\";
            String jsonString = PostmanCodeGenerator.generateAndWritePostmanCollection("买方代码申请", url, headers, jsonObject.toJSONString(), path+"postman_buycodeapply.json");
            jsonObject.clear();
            jsonObject = JSON.parseObject(jsonString);
            zxbLogger.info(jsonString);
            return new JsonResult<>(jsonObject);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("error", "无数据响应");
        return new JsonResult<>(jsonObject);
    }

    @PostMapping("/buycodeapply")
    public
    JsonResult<JSONObject> buycodeapply(@RequestParam String msg) throws Exception {
        if (msg != null) {
            JSONObject json = JSONObject.parseObject(msg);

            String recordid = json.getString("recordid");
            List<BuyerCodeApplyInfo> buyerCodeApplyInfoList = buyerCodeApplyService.getBuyerCodeApplylist("");
            String url = "{{host}}/ediserver/gateway.do";
            JSONArray headers = new JSONArray();
            headers.add(PostmanCodeGenerator.createHeader("Content-Type", "application/json"));
            //String body = "{\"key\":\"value\"}";
            JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(buyerCodeApplyInfoList));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("datas", jsonArray);
            jsonObject.put("imethod", "doBprBuyerCodeApply");
            HashMap<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json");
            zxbLogger.info("买方代码发送-->"+jsonObject.toJSONString());
            System.out.println(taxCloudConfig.getZxburl());

            String path=System.getProperty("user.dir")+"\\zxbbankAPI\\";
            PostmanCodeGenerator.generateAndWritePostmanCollection("买方代码申请", url, headers, jsonObject.toJSONString(), path+"postman_buycodeapply.json");
            try{
            String response = HttpUtil.httpPostJson(taxCloudConfig.getZxburl(), header, jsonObject.toJSONString(), false);

            jsonObject.clear();
            if (response != null) {
                // 成功
                System.out.println(response);
                jsonObject = JSONObject.parseObject(response);
            }
            } catch (Exception e) {
                jsonObject =  JSONObject.from(new ResultDTO("", ErrorMessage.ERR_SYSTEM, ErrorMessage.ERROR));
            }
            zxbLogger.info("买方代码发送回复-->"+jsonObject.toJSONString());
            return new JsonResult<>(jsonObject);

        }
        return new JsonResult<>(400, "查询失败");

    }
}









